/// Copyright (c) Microsoft Corporation.  All rights reserved.

using System;
using System.Collections.Generic;
using System.Windows.Forms;
using System.Security;
using System.Security.Policy;

namespace Microsoft.VSPowerToys.PackInstaller
{
    static class Program
    {
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main(string[] args)
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            try
            {
                string localFeedOverridePath = null;
                if (args.Length > 1)
                {
                    if (string.Equals(args[0], Strings.LocalFeedCommandLine, StringComparison.CurrentCultureIgnoreCase))
                    {
                        localFeedOverridePath = args[1];
                    }
                }
                Application.Run(new PackInstallerForm(localFeedOverridePath));
            }
            catch (SecurityException)
            {
                ShowGlobalAwareMessageBox(null, Strings.ErrorCaption, Strings.SecurityText, MessageBoxButtons.OK, MessageBoxIcon.Stop, MessageBoxDefaultButton.Button1);
            }
            catch (PolicyException)
            {
                ShowGlobalAwareMessageBox(null, Strings.ErrorCaption, Strings.SecurityText, MessageBoxButtons.OK, MessageBoxIcon.Stop, MessageBoxDefaultButton.Button1);
            }
        }

		/// <summary>
		/// This method is placed here so that it is accessible throughout the application.
		/// FxCop suggests that the RightToLeft properties be set appropriately based on the values
		/// of the parent controls.  Rather than repeat this logic everywhere a call to MessageBox.Show is used,
		/// it made more sense to write this method.  This only works for boxes that are OK boxes without much
		/// customization, anything fancier would need, either another method, or to have this method modified.
		/// </summary>
		static public DialogResult ShowGlobalAwareMessageBox(Control parentControl, string caption, string message)
		{
			return ShowGlobalAwareMessageBox(parentControl, caption, message, MessageBoxButtons.OK, MessageBoxIcon.None, MessageBoxDefaultButton.Button1);
		}

        /// <summary>
        /// This method is placed here so that it is accessible throughout the application.
        /// FxCop suggests that the RightToLeft properties be set appropriately based on the values
        /// of the parent controls.  Rather than repeat this logic everywhere a call to MessageBox.Show is used,
        /// it made more sense to write this method.  This only works for boxes that are OK boxes without much
        /// customization, anything fancier would need, either another method, or to have this method modified.
        /// </summary>
        static public DialogResult ShowGlobalAwareMessageBox(Control parentControl, string caption, string message, MessageBoxButtons button, MessageBoxIcon icon, MessageBoxDefaultButton defaultButton)
        {
            Control tempParentControl = parentControl;
            RightToLeft parentRtl = (tempParentControl == null) ? RightToLeft.No : tempParentControl.RightToLeft;

            while (tempParentControl != null && parentRtl == RightToLeft.Inherit)
            {
                tempParentControl = tempParentControl.Parent;
                parentRtl = tempParentControl.RightToLeft;
            }

            if (parentRtl != RightToLeft.Inherit)
            {
                MessageBoxOptions msgOptions = 0;

                if (parentRtl == RightToLeft.Yes)
                {
                    msgOptions = MessageBoxOptions.RtlReading | MessageBoxOptions.RightAlign;
                }

                return (MessageBox.Show(message, caption, button, icon, defaultButton, msgOptions));
            }
            else
            {
                return DialogResult.OK;
            }
        }
    }
}